<%
function trim(s)
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end
local dsp = require "luci.dispatcher"
-%>

<style>
#add_link_div{
	display: none;
	width: 30rem;
	position: absolute;
	left:50%;
	top:50%;
	transform: translate(-50%, -50%);
	padding-top: 30px;
	z-index: 99;
	text-align: center;
	background: white;
    box-shadow: darkgrey 10px 10px 30px 5px;
}
</style>

<script type="text/javascript">
	//<![CDATA[
	function ajax_add_node(link) {
		if (link) {
			XHR.get('<%=dsp.build_url("admin/services/passwall/link_add_node")%>', {
				'link': link
			},
			function(x, data) {
				if(x && x.status == 200) {
					window.location.href = '<%=dsp.build_url("admin/services/passwall/node_list")%>';
				}
				else {
					alert("<%:Error%>");
				}
			});
		}
	}
	
	function open_add_link_div() {
		document.getElementById("add_link_div").style.display = "block";
		document.getElementById("nodes_link").focus();
	}
	
	function close_add_link_div() {
		document.getElementById("add_link_div").style.display = "none";
	}
	
	function add_node() {
		var nodes_link = document.getElementById("nodes_link").value;
		if (nodes_link.trim() != "") {
			if (nodes_link.indexOf("ss://") == 0 || nodes_link.indexOf("ssr://") == 0 || nodes_link.indexOf("vmess://") == 0 || nodes_link.indexOf("trojan://") == 0) {
				ajax_add_node(nodes_link);
			}
			else {
				alert("<%:Please enter the correct link, ss:// ssr:// vmess:// trojan://%>");
			}
		}
		else {
			document.getElementById("nodes_link").focus();
		}
	}
	//]]>
</script>

<div class="cbi-value">
	<label class="cbi-value-title"><%:Add Node%></label>
	<div class="cbi-value-field">
		<input class="cbi-button cbi-button-add" type="button" onclick="open_add_link_div()" value="<%:Add the node via the link%>" />
	</div>
</div>

<div id="add_link_div">
	<div class="cbi-value">
		<label class="cbi-value-title"><%:SS/SSR/V2ray/Trojan Link%></label>
		<div class="cbi-value-field">
			<textarea id="nodes_link" rows="5" cols="50"></textarea>
		</div>
	</div>
	<div class="cbi-value">
		<label class="cbi-value-title"></label>
		<div class="cbi-value-field">
			<input class="cbi-button cbi-button-add" type="button" onclick="add_node()" value="<%:Add%>" />
			<input class="cbi-button cbi-button-remove" type="button" onclick="close_add_link_div()" value="<%:Close%>" />
		</div>
	</div>
</div>
